﻿#include "picturerectitem.h"

#include <QPainterPath>
#include <QPainter>
#include <QImageReader>
#include <QStyleOptionGraphicsItem>

// test
#include <QDebug>

PictureRectItem::PictureRectItem(const QString &path, int index, bool match)
    : QGraphicsRectItem(), mPicturePath(path), mPictureIndex(index), mMatchedFlag(match)
{
    init();
}

PictureRectItem::~PictureRectItem()
{
    qDebug() << "PictureRectItem::~PictureRectItem";
    if (mSourceImagetr != nullptr) delete mSourceImagetr;
}

void PictureRectItem::init()
{
    QFont font("Microsoft YaHei", 9);
    QFontMetrics metrics(font);
    mContainMargin = metrics.height() * 0.5;

    setFlags(ItemIsSelectable | ItemIsMovable);
    setAcceptHoverEvents(true);

    QImageReader reader;
    reader.setFileName(mPicturePath);

    // 图像宽高
    mSourceWidth = reader.size().width();
    mSourceHeight = reader.size().height();

    // 图像数据 (此处储存在,耗费的内存基本就是图形的总大小，但是在操作的时候，很流畅了)
    // 反之，如果在 paint 函数中用临时变量重新获取图像，那么不耗费内存，但是操作会卡顿
    mSourceImagetr = new QImage(reader.read());
}

void PictureRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option); Q_UNUSED(widget);
    if (nullptr == mSourceImagetr) return;
   painter->setRenderHint(QPainter::SmoothPixmapTransform);

   painter->drawImage(boundingRect(), *mSourceImagetr);

   if (!mMatchedFlag)
   {
       QColor borderColor = QColor(255, 0, 0);
       painter->setRenderHint(QPainter::Antialiasing);
       painter->setBrush(Qt::NoBrush);
       painter->setPen(QPen(QBrush(borderColor), 6));
       QRectF rect = boundingRect();
       rect.adjust(3, 3, -3, -3);
       painter->drawRect(rect);

       // 绘制 序号
       QString seq = QString::number(mPictureIndex);
       painter->setPen(Qt::NoPen);
       painter->setBrush(QBrush(QColor(79, 159, 96)));
       QRect rectSeq = QRect(boundingRect().x() + mContainMargin, boundingRect().y() + mContainMargin, boundingRect().width() * 0.2, boundingRect().width() * 0.2);

       QPen pen;
       pen.setBrush(QBrush(QColor(254, 254, 254)));
       pen.setWidth(2);
       painter->setPen(pen);
       painter->setFont(QFont("Microsoft Yahei", 120, 360));

       painter->setRenderHint(QPainter::TextAntialiasing);
       painter->drawText(rectSeq, Qt::AlignCenter, seq);
   }
   else if (option->state & QStyle::State_Selected)
   {
        QColor borderColor = QColor(252, 145, 83);
        painter->setRenderHint(QPainter::Antialiasing);
        painter->setPen(QPen(QBrush(borderColor), 6));
        painter->setBrush(Qt::NoBrush);
        QRectF rect = boundingRect();
        rect.adjust(3, 3, -3, -3);
        painter->drawRect(rect);

        // 绘制 序号
        QString seq = QString::number(mPictureIndex);
        painter->setPen(Qt::NoPen);
        painter->setBrush(QBrush(QColor(79, 159, 96)));
        QRect rectSeq = QRect(boundingRect().x() + mContainMargin, boundingRect().y() + mContainMargin, boundingRect().width() * 0.2, boundingRect().width() * 0.2);

        QPen pen;
        pen.setBrush(QBrush(QColor(254, 254, 254)));
        pen.setWidth(2);
        painter->setPen(pen);
        painter->setFont(QFont("Microsoft Yahei", 120, 360));

        painter->setRenderHint(QPainter::TextAntialiasing);
        painter->drawText(rectSeq, Qt::AlignCenter, seq);
   }
   else if (option->state & QStyle::State_MouseOver)
   {
       QColor borderColor = QColor(79, 159, 96);
       painter->setRenderHint(QPainter::Antialiasing);
       painter->setPen(QPen(QBrush(borderColor), 6));
       painter->setBrush(Qt::NoBrush);
       QRectF rect = boundingRect();
       rect.adjust(3, 3, -3, -3);
       painter->drawRect(rect);

       // 绘制 序号
       QString seq = QString::number(mPictureIndex);
       painter->setPen(Qt::NoPen);
       painter->setBrush(QBrush(QColor(79, 159, 96)));
       QRect rectSeq = QRect(boundingRect().x() + mContainMargin, boundingRect().y() + mContainMargin, boundingRect().width() * 0.2, boundingRect().width() * 0.2);

       QPen pen;
       pen.setBrush(QBrush(QColor(254, 254, 254)));
       pen.setWidth(2);
       painter->setPen(pen);
       painter->setFont(QFont("Microsoft Yahei", 120, 360));

       painter->setRenderHint(QPainter::TextAntialiasing);
       painter->drawText(rectSeq, Qt::AlignCenter, seq);
   }


}

QRectF PictureRectItem::boundingRect() const
{
    return QRectF(0, 0, mSourceWidth, mSourceHeight);
}

QPainterPath PictureRectItem::shape() const
{
    QPainterPath path;
    path.addRect(0, 0, mSourceWidth, mSourceHeight);
    return path;
}
